package cn.innosmart.aq.adapter;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.TextViewCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.LvMenuItem;

/**
 * Box列表界面的左划菜单适配器
 * Created by David on 2015/7/27.
 */
public class BoxListMenuItemAdapter extends BaseAdapter {
    private final int mIconSize;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<LvMenuItem> mItems;
    private View.OnClickListener onClickListener;

    public BoxListMenuItemAdapter(Context context, List<LvMenuItem> list) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        mIconSize = AutoUtils.getPercentWidthSize(70);//24dp

        mItems = list;
    }

    /**
     * 设置菜单
     *
     * @param list
     */
    private void setMenu(ArrayList list) {
        mItems = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LvMenuItem item = mItems.get(position);
        switch (item.type) {
            case LvMenuItem.TYPE_NORMAL:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item, parent,
                            false);
                }
                TextView itemView = (TextView) convertView.findViewById(R.id.tv_title);
                itemView.setTextColor(convertView.getResources().getColor(R.color.poup_font_grey));
                itemView.setText(item.name);
                Drawable icon = mContext.getResources().getDrawable(item.icon);
                if (icon != null) {
                    icon.setBounds(0, 0, mIconSize, mIconSize);
                    TextViewCompat.setCompoundDrawablesRelative(itemView, icon, null, null, null);
                }
                itemView.setTag(position);
                itemView.setOnClickListener(onClickListener);
                break;
            case LvMenuItem.TYPE_NO_ICON:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item_subheader,
                            parent, false);
                }
                TextView subHeader = (TextView) convertView.findViewById(R.id.tv_title);
                subHeader.setText(item.name);
                break;
            case LvMenuItem.TYPE_SEPARATOR:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item_separator,
                            parent, false);
                }
                break;
        }
        AutoUtils.autoSize(convertView);
        return convertView;
    }

    /**
     * 设置菜单Icon颜色
     *
     * @param icon
     */
    public void setIconColor(Drawable icon) {
        int textColorSecondary = android.R.attr.textColorSecondary;
        TypedValue value = new TypedValue();
        if (!mContext.getTheme().resolveAttribute(textColorSecondary, value, true)) {
            return;
        }
        int baseColor = mContext.getResources().getColor(value.resourceId);
        icon.setColorFilter(baseColor, PorterDuff.Mode.MULTIPLY);
    }

    /**
     * 设置点击事件监听器
     *
     * @param onClickListener
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

}